package main

import (
    "fmt"
    "errors"
)

// PaymentMethod 支付方式接口
type PaymentMethod interface {
    ProcessPayment(amount float64) error
    GetPaymentType() string
    ValidatePayment() bool
}

// CreditCardPayment 信用卡支付
type CreditCardPayment struct {
    CardNumber string
    CVV        string
}

func (c *CreditCardPayment) ProcessPayment(amount float64) error {
    if !c.ValidatePayment() {
        return errors.New("信用卡验证失败")
    }
    fmt.Printf("使用信用卡支付 $%.2f\n", amount)
    return nil
}

func (c *CreditCardPayment) GetPaymentType() string {
    return "信用卡"
}

func (c *CreditCardPayment) ValidatePayment() bool {
    return len(c.CardNumber) == 16 && len(c.CVV) == 3
}

// PayPalPayment PayPal支付
type PayPalPayment struct {
    Email string
}

func (p *PayPalPayment) ProcessPayment(amount float64) error {
    if !p.ValidatePayment() {
        return errors.New("PayPal账户验证失败")
    }
    fmt.Printf("使用PayPal支付 $%.2f\n", amount)
    return nil
}

func (p *PayPalPayment) GetPaymentType() string {
    return "PayPal"
}

func (p *PayPalPayment) ValidatePayment() bool {
    return len(p.Email) > 0 && len(p.Email) < 100
}

// PaymentFactory 支付工厂
type PaymentFactory struct{}

// CreatePayment 创建支付方式
func (f *PaymentFactory) CreatePayment(paymentType string, config map[string]string) (PaymentMethod, error) {
    switch paymentType {
    case "credit_card":
        return &CreditCardPayment{
            CardNumber: config["card_number"],
            CVV:        config["cvv"],
        }, nil
    case "paypal":
        return &PayPalPayment{
            Email: config["email"],
        }, nil
    default:
        return nil, fmt.Errorf("不支持的支付方式: %s", paymentType)
    }
}

// 使用演示
func main() {
    fmt.Println("=== 支付方式工厂演示 ===")
    
    factory := &PaymentFactory{}
    
    // 创建信用卡支付
    creditCardConfig := map[string]string{
        "card_number": "1234567890123456",
        "cvv":         "123",
    }
    creditCard, _ := factory.CreatePayment("credit_card", creditCardConfig)
    creditCard.ProcessPayment(99.99)
    
    // 创建PayPal支付
    paypalConfig := map[string]string{
        "email": "user@example.com",
    }
    paypal, _ := factory.CreatePayment("paypal", paypalConfig)
    paypal.ProcessPayment(149.99)
}